/*
 * This file is part of "Deathwatch Ammo Counter for Android".
 *
 * "Deathwatch Ammo Counter for Android" is free software: you can redistribute
 * it and/or modify it under the terms of the GNU General Public License as 
 * published by the Free Software Foundation, either version 3 of the License, 
 * or (at your option) any later version.
 *
 * "Deathwatch Ammo Counter for Android" is distributed in the hope that it 
 * will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty 
 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General 
 * Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with "Deathwatch Ammo Counter for Android".  
 * If not, see <http://www.gnu.org/licenses/>.
 * 
 */
package com.TikiiTorch.Deathwatch.AmmoCounter;

import java.util.ArrayList;

import android.os.Parcel;
import android.os.Parcelable;

public class Weapon implements Parcelable {

	public static final int RATE_OF_FIRE_SINGLE = 0;
	public static final int RATE_OF_FIRE_SEMI = 1;
	public static final int RATE_OF_FIRE_FULL = 2;

	private String name;

	private String ammoType;

	private int clipSize;

	private String group;

	private int loadedAmmo = 0;

	private ArrayList<AmmoLoad> ammoSupply;

	private int[] rof = { 0, 0, 0 };

	/**
	 * Creates an empty instance of a weapon
	 */
	public Weapon() {
		ammoSupply = new ArrayList<AmmoLoad>();
	}

	/**
	 * Creates an instance of a weapon as defined
	 * 
	 * @param name
	 *            The name of this weapon
	 * @param clipSize
	 *            The clip size of this weapon
	 * @param rof
	 *            The rate of fire for this weapon
	 */
	public Weapon(String name, String ammoType, int clipSize, int[] rof) {
		this.name = name;
		this.ammoType = ammoType;
		this.clipSize = clipSize;
		this.rof = rof;
		ammoSupply = new ArrayList<AmmoLoad>();
	}

	public Weapon(Parcel parcel) {

		this.name = parcel.readString();
		this.ammoType = parcel.readString();
		this.clipSize = parcel.readInt();
		this.rof = new int[] { parcel.readInt(), parcel.readInt(),
				parcel.readInt() };
		ammoSupply = new ArrayList<AmmoLoad>();
	}

	/**
	 * Gets the name of this weapon
	 * 
	 * @return The name of this weapon
	 */
	public String getName() {
		return this.name;
	}

	/**
	 * Sets the name of this weapon
	 * 
	 * @param name
	 *            The name of this weapon
	 */
	public void setName(String name) {
		this.name = name;
	}

	/**
	 * Gets the ammo type for this weapon
	 * 
	 * @return The ammo type for this weapon
	 */
	public String getAmmoType() {
		return this.ammoType;
	}

	/**
	 * Sets the ammo type for this weapon
	 * 
	 * @param ammoType
	 *            The ammo type for this weapon
	 */
	public void setAmmoType(String ammoType) {
		this.ammoType = ammoType;
	}

	/**
	 * Gets the size of the clip for this weapon
	 * 
	 * @return The size of the clip for this weapon
	 */
	public int getClipSize() {
		return this.clipSize;
	}

	/**
	 * Sets the size of the clip for this weapon
	 * 
	 * @param clipSize
	 *            The size of the clip for this weapon
	 */
	public void setClipSize(int clipSize) {
		this.clipSize = clipSize;
	}

	public String getGroup() {
		return this.group;
	}

	public void setGroup(String group) {
		this.group = group;
	}

	/**
	 * Gets the rate of fire for this weapon
	 * 
	 * @return The rate of fire for this weapon
	 */
	public int[] getROF() {
		return this.rof;
	}

	/**
	 * Sets the rate of fire for this weapon
	 * 
	 * @param rof
	 *            The rate of fire for this weapon
	 */
	public void setROF(int[] rof) {
		this.rof = rof;
	}

	/**
	 * Gets the rate of fire for this weapon at a specific shot type
	 * 
	 * @param shotType
	 *            The shot type to find the rate of fire for
	 * @return The rate of fire for this weapon at a specific shot type
	 */
	public int getROF(int shotType) {
		return rof[shotType];
	}

	/**
	 * Gets the index of the ammo types that is currently loaded in the weapon
	 * 
	 * @return The index of the ammo types that is currently loaded in the
	 *         weapon
	 */
	public int getLoadedAmmo() {
		if (ammoSupply.size() > 0) {
			return this.loadedAmmo;
		} else {
			return -1;
		}
	}

	public void setLoadedAmmo(int index) {
		this.loadedAmmo = index;
	}

	public void addAmmoLoad(AmmoLoad ammoSupply) {
		this.ammoSupply.add(ammoSupply);
	}

	public void removeammoLoad(int index) {
		this.ammoSupply.remove(index);
	}

	public ArrayList<AmmoLoad> getAmmoLoads() {
		return this.ammoSupply;
	}

	public void reload() {
		ammoSupply.get(loadedAmmo).reloadClip();
	}

	public void fire(int shotCount) {
		if (this.ammoSupply.get(loadedAmmo).getClipAmount() > 0) {
			ammoSupply.get(loadedAmmo).expendRounds(shotCount);
		}
	}

	@Override
	public String toString() {
		return this.name;
	}

	@Override
	public int describeContents() {
		return this.hashCode();
	}

	@Override
	public void writeToParcel(Parcel dest, int flags) {

		dest.writeString(name);
		dest.writeString(ammoType);
		dest.writeInt(clipSize);
		dest.writeInt(rof[0]);
		dest.writeInt(rof[1]);
		dest.writeInt(rof[2]);

	}

	public static final Parcelable.Creator<Weapon> CREATOR = new Parcelable.Creator<Weapon>() {
		public Weapon createFromParcel(Parcel source) {
			return new Weapon(source);
		}

		public Weapon[] newArray(int size) {
			return new Weapon[size];
		}
	};

}
